package com.example.renshiguanli.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.renshiguanli.config.PaginationConstant;
import com.example.renshiguanli.mapper.EmpMapper;
import com.example.renshiguanli.mapper.EmprpMapper;
import com.example.renshiguanli.pojo.Emp;
import com.example.renshiguanli.pojo.Emprp;
import com.example.renshiguanli.service.EmprpService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class EmprpController {
    @Autowired
    private EmprpService emprpService;
    @Autowired
    private EmprpMapper emprpMapper;
    @Autowired
    private EmpMapper empMapper;
    @GetMapping("/emprps")
    public String findAll(Model model, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        if(ObjectUtils.isEmpty(pageNum)){
            pageNum=PaginationConstant.CURRENT_NUM;
        }
        PageHelper.startPage(pageNum, PaginationConstant.PAGE_SIZE);
//        查询数据
        List<Emprp> allEmprp = emprpService.findAllEmprp();
        model.addAttribute("emprp",allEmprp);
        PageInfo<Emprp> pageInfo=new PageInfo<>(allEmprp);
        model.addAttribute("pageInfo",pageInfo);
        return "emprp/emprp";
    }
//    添加奖惩管理
//    展示信息
    @GetMapping("/emprp")
    public String findEmprp(Model model){
        List<Emp> allEmps = empMapper.getAllEmps();
        model.addAttribute("emps",allEmps);
        return "emprp/add";
    }
//    添加信息
    @PostMapping("/emprp")
    public String add(Emprp emprp){
        emprpMapper.addEmprp(emprp);
        return "redirect:/emprps";
    }
//    编辑
//    根据id查询信息
    @GetMapping("/emprp/{id}")
    public String find(@PathVariable("id") Integer id,Model model){
        Emprp emprp = emprpMapper.findById(id);
        model.addAttribute("emprp",emprp);
        List<Emp> allEmps = empMapper.getAllEmps();
        model.addAttribute("emps",allEmps);
        return "emprp/add";
    }
//    修改信息
    @PutMapping("/emprp")
    public String update(Emprp emprp){
        emprpMapper.updateEmprp(emprp);
        return "redirect:/emprps";
    }
//    删除信息
    @DeleteMapping("/emprp/{id}")
    public String delete(@PathVariable Integer id){
        emprpMapper.deleteEmprp(id);
        return "redirect:/emprps";
    }
//    查找信息
    @PostMapping("/rpsearch")
    public String rpsearch(String name,@RequestParam(value="pageNum",defaultValue="1") Integer pageNum,Model model){
        PageHelper.startPage(pageNum,PaginationConstant.PAGE_SIZE);
        List<Emprp> query = emprpService.query(name);
        model.addAttribute("emprp",query);
        PageInfo<Emprp> pageInfo=new PageInfo<>(query);
        model.addAttribute("pageInfo",pageInfo);
        return "emprp/emprp";
    }
}
